﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using nutricion;
using System.Drawing.PieChart;
using System.Collections;

namespace NutriFast
{
    
    public partial class DietaSelComida : Form
    {
        static int Cs = 8;
        static string meal;
        DietaUpdatePacient a;
        public static NumericUpDown[] Por = new NumericUpDown[Cs];
        public static Button[] C = new Button[Cs];
        public static Label[] Pt = new Label[Cs];
        public static Label[] Lip = new Label[Cs];
        public static Label[] CHO = new Label[Cs];
        public static Label LPorT = new Label();
        public static Label LCT = new Label();
        public static Label LPtT = new Label();
        public static Label LLipT = new Label();
        public static Label LCHOT = new Label();
        public static Label RLPorT = new Label();
        public static Label RLCT = new Label();
        public static Label RLPtT = new Label();
        public static Label RLLipT = new Label();
        public static Label RLCHOT = new Label();
        public static decimal t = 100;
        int currentpacient;

        public DietaSelComida(int c,int Comidas,DietaUpdatePacient a)
        {
            this.a = a;
            Cs = Comidas;
            this.currentpacient = c;
            InitializeComponent();
           
        }


        private void InitializeChart()
        {
            SetValues();
            SetPieDisplacements();
            SetColors();
            SetTexts();
            m_panelDrawing.LeftMargin = 5;
            m_panelDrawing.RightMargin = 5;
            m_panelDrawing.TopMargin = 5;
            m_panelDrawing.BottomMargin = 5;
            m_panelDrawing.FitChart = false;
            m_panelDrawing.SliceRelativeHeight = (float)0.25;
            m_panelDrawing.InitialAngle = (float)-30;
            m_panelDrawing.EdgeLineWidth = (float)1;
        }


        #region SETERS y VALUES

        private void SetValues()
        {
            m_panelDrawing.Values = Values;
        }

        private void SetPieDisplacements()
        {
            m_panelDrawing.SliceRelativeDisplacements = Displacements;
        }

        private void SetColors()
        {
            m_panelDrawing.Colors = Colors;
        }

        private void SetTexts()
        {
            m_panelDrawing.Texts = Texts;
        }

        private decimal[] Values
        {
            get
            {
                ArrayList values = new ArrayList();
                for(int i=0;i < Cs;i++){
                    values.Add(Por[i].Value);   
                }
                
             return (decimal[])values.ToArray(typeof(decimal));
            }
        }

        private float[] Displacements
        {
            get
            {
                ArrayList displacements = new ArrayList();
                for (int i = 1; i < Cs; i++)
                {
                    
                    displacements.Add((float).1);
                    
                }
                return (float[])displacements.ToArray(typeof(float));
            }
        }

        private string[] Texts
        {
            get
            {
                ArrayList texts = new ArrayList();
                for (int i = 0; i < Cs; i++)
                {
                    texts.Add("Comida "+(i+1));
                }
                return (string[])texts.ToArray(typeof(string));
            }
        }

        private Color[] Colors
        {
            get
            {
                ArrayList colors = new ArrayList();
                colors.Add(Color.FromArgb((int)100, Color.Red));
                colors.Add(Color.FromArgb((int)100, Color.Yellow));
                colors.Add(Color.FromArgb((int)100,Color.Blue));
                colors.Add(Color.FromArgb((int)100, Color.Green));
                colors.Add(Color.FromArgb((int)100, Color.Orange));
                colors.Add(Color.FromArgb((int)100, Color.Pink));
                colors.Add(Color.FromArgb((int)100, Color.Turquoise));
                colors.Add(Color.FromArgb((int)100, Color.Gray));
                return (Color[])colors.ToArray(typeof(Color));
            }
        }

        #endregion



        public void DietaSelComida_Load(object sender, EventArgs e)
        {
            //Cs = Ca;
            m_panelDrawing.ShadowStyle = ShadowStyle.NoShadow;
            m_panelDrawing.EdgeColorType = (EdgeColorType)3;

            label1.Text = "KiloCalorias Totales: " + dbVic.buscarPorcentajes("Kilo", dbVic.buscarDieta());
            #region LABELS
           
            ///////////Botones
            Label LC = new Label();
            LC.Text = "Comidas";
            LC.Name = "LC";
            LC.Size = new Size(75, 25);
            LC.Location = new Point(20 , 22);
            LC.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(LC);
            //////////Proteinas
            Label LPt = new Label();
            LPt.Text = "Proteinas";
            LPt.Name = "LPt";
            LPt.Size = new Size(75, 25);
            LPt.Location = new Point(20, 55);
            LPt.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(LPt);
            /////////////Lipidos
            Label LLip = new Label();
            LLip.Text = "Lipidos";
            LLip.Name = "LLip";
            LLip.Size = new Size(75, 25);
            LLip.Location = new Point(20 , 88);
            LLip.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(LLip);
            //////////////CHO
            Label LCHO = new Label();
            LCHO.Text = "Carbohidratos";
            LCHO.Name = "LCHO";
            LCHO.Size = new Size(75, 25);
            LCHO.Location = new Point(20 , 131);
            LCHO.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(LCHO);
            ////////////%%%%%%
            Label LPor = new Label();
            LPor.Text = "Porcentajes";
            LPor.Name = "L%";
            LPor.Size = new Size(75, 25);
            LPor.Location = new Point(20, 164);
            LPor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(LPor);
            #endregion

            #region TOTALES
            ///////////Botones
            LCT.Text = "Totales";
            LCT.Name = "LCT";
            LCT.Size = new Size(75, 25);
            LCT.Location = new Point(20 + ((Cs + 1) * 80), 22);
            LCT.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(LCT);
            //////////Proteinas

            LPtT.Text = "" + Decimal.Round((decimal)Formulas.kilocaloriasGramoProteinas(dbVic.buscarPorcentajes("Kilo", dbVic.buscarDieta()), dbVic.buscarPorcentajes("proteinas", dbVic.buscarDieta())),2);
            LPtT.Name = "LPtT";
            LPtT.Size = new Size(75, 25);
            LPtT.Location = new Point(20 + ((Cs + 1) * 80), 55);
            LPtT.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(LPtT);
            /////////////Lipidos

            LLipT.Text = "" + Decimal.Round((decimal)Formulas.kilocaloriasGramoLipidos(dbVic.buscarPorcentajes("Kilo", dbVic.buscarDieta()), dbVic.buscarPorcentajes("lipidos", dbVic.buscarDieta())),2);
            LLipT.Name = "LLipT";
            LLipT.Size = new Size(75, 25);
            LLipT.Location = new Point(20 + ((Cs + 1) * 80), 88);
            LLipT.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(LLipT);
            //////////////CHO

            LCHOT.Text =  "" + Decimal.Round((decimal)Formulas.kilocaloriasGramoCho(dbVic.buscarPorcentajes("Kilo", dbVic.buscarDieta()), dbVic.buscarPorcentajes("cho", dbVic.buscarDieta())),2);
            LCHOT.Name = "LCHOT";
            LCHOT.Size = new Size(75, 25);
            LCHOT.Location = new Point(20 + ((Cs + 1) * 80), 131);
            LCHOT.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(LCHOT);
            ////////////%%%%%%
            
            LPorT.Text = "100"+"%";
            LPorT.Name = "L%T";
            LPorT.Size = new Size(75, 25);
            LPorT.Location = new Point(20 + ((Cs + 1) * 80), 164);
            LPorT.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(LPorT);
            #endregion

            #region RECOMENDADO
            ///////////Botones
            RLCT.Text = "Tabla";
            RLCT.Name = "RLCT";
            RLCT.Size = new Size(75, 25);
            RLCT.Location = new Point(20 + ((Cs + 2) * 80), 22);
            RLCT.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(RLCT);
            //////////Proteinas

            RLPtT.Text = "" + Decimal.Round((decimal)Formulas.kilocaloriasGramoProteinas(dbVic.buscarPorcentajes("Kilo", dbVic.buscarDieta()), dbVic.buscarPorcentajes("proteinas", dbVic.buscarDieta())), 2);
            RLPtT.Name = "RLPtT";
            RLPtT.Size = new Size(75, 25);
            RLPtT.Location = new Point(20 + ((Cs + 2) * 80), 55);
            RLPtT.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(RLPtT);
            /////////////Lipidos

            RLLipT.Text = "" + Decimal.Round((decimal)Formulas.kilocaloriasGramoLipidos(dbVic.buscarPorcentajes("Kilo", dbVic.buscarDieta()), dbVic.buscarPorcentajes("lipidos", dbVic.buscarDieta())), 2);
            RLLipT.Name = "RLLipT";
            RLLipT.Size = new Size(75, 25);
            RLLipT.Location = new Point(20 + ((Cs + 2) * 80), 88);
            RLLipT.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(RLLipT);
            //////////////CHO

            RLCHOT.Text = "" + Decimal.Round((decimal)Formulas.kilocaloriasGramoCho(dbVic.buscarPorcentajes("Kilo", dbVic.buscarDieta()), dbVic.buscarPorcentajes("cho", dbVic.buscarDieta())), 2);
            RLCHOT.Name = "RLCHOT";
            RLCHOT.Size = new Size(75, 25);
            RLCHOT.Location = new Point(20 + ((Cs + 2) * 80), 131);
            RLCHOT.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(RLCHOT);
            ////////////%%%%%%

            RLPorT.Text = "";
            RLPorT.Name = "L%T";
            RLPorT.Size = new Size(75, 25);
            RLPorT.Location = new Point(20 + ((Cs + 2) * 80), 164);
            RLPorT.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(RLPorT);
            #endregion

            #region DATOS

            for (int i=0;i < Cs;i++){

                ///////////Botones
                C[i] = new Button();
                C[i].Text = "Comida " + (i + 1);
                C[i].Name = "" + (i + 1);
                C[i].Size = new Size(75, 25);
                C[i].Location = new Point(20 + ((i + 1) * 80), 22);
                C[i].Click += new EventHandler(C_Click);
                this.Controls.Add(C[i]);

                ////////////%%%%%%
                Por[i] = new NumericUpDown();
                float s = 100 / Cs;
                Por[i].Text = "" + s;
                Por[i].Name = ""+(i + 1);
                Por[i].Size = new Size(75, 25);
                Por[i].Location = new Point(20 + ((i + 1) * 80), 164);
                Por[i].TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
                this.Controls.Add(Por[i]);
                Por[i].TextChanged += new System.EventHandler(this.Por_TextChanged);

                //////////Proteinas
                Pt[i] = new Label();
                Pt[i].Text = ""+ decimal.Round(decimal.Parse(LPtT.Text)/Cs,2);
                Pt[i].Name = "Pt" + (i + 1);
                Pt[i].Size = new Size(75, 25);
                Pt[i].Location = new Point(20 + ((i + 1) * 80), 55);
                Pt[i].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                this.Controls.Add(Pt[i]);

                /////////////Lipidos
                Lip[i] = new Label();
                Lip[i].Text = "" + decimal.Round(decimal.Parse(LLipT.Text) / Cs, 2);
                Lip[i].Name = "Lip" + (i + 1);
                Lip[i].Size = new Size(75, 25);
                Lip[i].Location = new Point(20 + ((i + 1) * 80), 88);
                Lip[i].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                this.Controls.Add(Lip[i]);

                //////////////CHO
                CHO[i] = new Label();
                CHO[i].Text = "" + decimal.Round(decimal.Parse(LCHOT.Text) / Cs, 2);
                CHO[i].Name = "CHO" + (i + 1);
                CHO[i].Size = new Size(75, 25);
                CHO[i].Location = new Point(20 + ((i + 1) * 80), 131);
                CHO[i].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                this.Controls.Add(CHO[i]);
            }
            #endregion


            this.ClientSize = new System.Drawing.Size(200+((Cs+1)*80), 500);
            InitializeChart();
        }

        private void C_Click(object sender, EventArgs e) {
            //(id dieta,id meal, proteinas,para el meal, lipidos, CHO)
            ((Button)sender).Enabled = false;
            meal=((Button)sender).Name.ToString();
            dbTavo.mealAdd(int.Parse(((Button)sender).Name.ToString()),dbVic.buscarDieta(),float.Parse(Pt[int.Parse(((Button)sender).Name.ToString())-1].Text));
            Console.WriteLine(int.Parse(((Button)sender).Name.ToString()));
            Console.WriteLine(dbVic.buscarDieta());
            Console.WriteLine(float.Parse(Pt[int.Parse(((Button)sender).Name.ToString()) - 1].Text));
            DietasComidas DietCom = new DietasComidas(this,dbVic.buscarDieta(), int.Parse(((Button)sender).Name.ToString()), float.Parse(Pt[int.Parse(((Button)sender).Name.ToString())-1].Text), float.Parse(Lip[int.Parse(((Button)sender).Name.ToString())-1].Text), float.Parse(CHO[int.Parse(((Button)sender).Name.ToString())-1].Text));
            DietCom.MdiParent = this.MdiParent;
            DietCom.Show();
        }

        public int IDComida {

            get { return int.Parse(""+meal); }
        }

        private void Por_TextChanged(object sender, EventArgs e)
        {   
            SetValues();
            #region Declaracion De Variables
            t = 0;
            decimal tPt = 0;
            decimal tLip = 0;
            decimal tCHO = 0;
            #endregion

            #region Llenado de Totales
            for (int i = 0; i < Cs; i++)
            {
                t += Por[i].Value;    
            }
            Pt[int.Parse(((NumericUpDown)sender).Name.ToString()) - 1].Text = "" + Decimal.Round(( decimal.Parse(((NumericUpDown)sender).Text.ToString())/ 100) * decimal.Parse(RLPtT.Text), 2);
            Lip[int.Parse(((NumericUpDown)sender).Name.ToString()) - 1].Text = "" + Decimal.Round((( decimal.Parse(((NumericUpDown)sender).Text.ToString()) / 100) * decimal.Parse(RLLipT.Text)), 2);
            CHO[int.Parse(((NumericUpDown)sender).Name.ToString()) - 1].Text = "" + Decimal.Round((( decimal.Parse(((NumericUpDown)sender).Text.ToString()) / 100) * decimal.Parse(RLCHOT.Text)),2);
            #endregion


            for (int i = 0; i < Cs; i++)
            {
                tPt += decimal.Parse(Pt[i].Text);
                tLip += decimal.Parse(Lip[i].Text);
                tCHO += decimal.Parse(CHO[i].Text);
            }

            LPorT.Text = t + "%";
            LPtT.Text = "" + Decimal.Round(tPt,2);
            LLipT.Text = "" + Decimal.Round(tLip, 2);
            LCHOT.Text = "" + Decimal.Round(tCHO,2);

            if (t > 110)
                LPorT.BackColor = Color.Red;
            else if(t<90)
                LPorT.BackColor = Color.Yellow;
            else
                LPorT.BackColor = System.Drawing.SystemColors.Control;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            a.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (t > 110)
            {
                if (MessageBox.Show("El porcentaje es mayor al recomendado, seguro de que desea continuar ?", "Alerta", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    DietaSelReceta die = new DietaSelReceta(currentpacient,dbVic.buscarDieta(), Cs);
                    die.MdiParent = this.MdiParent;
                    this.Hide();
                    die.Show();
                }
            }
            else if (t < 90)
            {
                if (MessageBox.Show("El porcentaje es menor al recomendado, seguro de que desea continuar ?", "Alerta", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    DietaSelReceta die = new DietaSelReceta(currentpacient,dbVic.buscarDieta(), Cs);
                    die.MdiParent = this.MdiParent;
                    this.Hide();
                    die.Show();
                }
            }
            else
            {
                DietaSelReceta die = new DietaSelReceta(currentpacient,dbVic.buscarDieta(), Cs);
                die.MdiParent = this.MdiParent;
                this.Hide();
                die.Show();
            }
        }

       
    }
}
